import Vue from 'vue'
import Vuex from 'vuex'
import storage from "@/utils/storage";
import {asyncRouterMap, constantRouterMap} from '@/utils/router';

Vue.use(Vuex);

export default new Vuex.Store({
    state: {
        active: '',//菜单选中,主要针对F5刷新后菜单的默认选中
        loading: false,//加载效果
        upload: '/manage/upload',//上传图片地址
        routers: [],//权限筛选后的路由
    },
    getters: {
        active:(state)=> {return state.active;},
        loading:(state)=> {return state.loading;},
        upload:(state)=> {return state.upload;},
        routers:(state)=> {return state.routers;},
    },
    mutations: {
        SET_ACTIVE(state,param){state.active = param;},
        SET_LOADING(state,param){state.loading = param;},
        SET_ROUTERS(state,param){state.routers = param;},
        USER_LOGIN(state,param){
            storage.set('token',param.token);
            storage.set('userinfo',param.userinfo);
        },
        USER_LOGOUT(){
            storage.remove('token');
            storage.remove('userinfo');
        },
    },
    actions: {
        //开启加载效果
        setLoading({commit},param){commit('SET_LOADING',param);},
        //设置菜单选中
        setActive({commit},param){commit('SET_ACTIVE',param);},
        //登陆
        userLogin({commit},param){
            return new Promise((resolve) => {
                commit('USER_LOGIN',param);
                resolve();
            });
        },
        //退出登录
        userLogout({commit}){
            return new Promise((resolve) => {
                commit('USER_LOGOUT');
                resolve();
            });
        },
        generateRoutes({commit}){
            //全部执行完再返回
            return new Promise(resolve => {
                const accessedRouters = asyncRouterMap.filter(v => {
                    return true;
                });
                commit('SET_ROUTERS', accessedRouters);
                resolve();
            });
        },
    }
})
